package com.fanfanwang.ffw.app.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.R.color;
import com.fanfanwang.ffw.app.bean.DianListBean;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.ui.home.RoutePlanDemo;

public class ServiceCarWashListAdapter extends BaseAdapter {

	private ArrayList<DianListBean> beans;
	private Context mContext;

	public ServiceCarWashListAdapter(Context mContext,
			ArrayList<DianListBean> beans) {
		// TODO Auto-generated constructor stub
		this.mContext = mContext;
		this.beans = beans;
	}

	public void resetData(ArrayList<DianListBean> beans) {
		this.beans = beans;
	}

	public void addList(ArrayList<DianListBean> beans) {
		this.beans.addAll(beans);
	}

	public void clearList() {
		this.beans.clear();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return beans.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(
					R.layout.item_for_service_carwashlist, null);
			holder = new ViewHolder();
			holder.id = (TextView) convertView.findViewById(R.id.wash_num);
			holder.name = (TextView) convertView.findViewById(R.id.wash_title);
			holder.juli = (TextView) convertView
					.findViewById(R.id.wash_distance);
			holder.addr = (TextView) convertView
					.findViewById(R.id.wash_address);
			holder.iv_tel = (ImageView) convertView.findViewById(R.id.iv_tel);
			holder.tv_tel = (TextView) convertView.findViewById(R.id.tv_tel);
			holder.rl_tohere = (RelativeLayout) convertView
					.findViewById(R.id.rl_tohere);
			holder.rl_call = (RelativeLayout) convertView
					.findViewById(R.id.rl_call);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		/*
		 * holder.id.setText((position+1)+"."); if(position==1) {
		 * holder.tv_tel.setTextColor(color.textview_hint);
		 * holder.iv_tel.setImageResource(R.drawable.iv_tel_not_click); }
		 */
		if (!TextUtils.isEmpty(beans.get(position).id)) {
			holder.id.setText(beans.get(position).id);
		}
		if (!TextUtils.isEmpty(beans.get(position).name)) {
			holder.name.setText(AppConstant.URLDecoderString(beans
					.get(position).name));
		}
		if (!TextUtils.isEmpty(beans.get(position).juli)) {
			holder.juli.setText(AppConstant.URLDecoderString(beans
					.get(position).juli) + "千米");
		}
		if (!TextUtils.isEmpty(beans.get(position).address)) {
			holder.addr.setText(AppConstant.URLDecoderString(beans
					.get(position).address));
		}
		if (TextUtils.isEmpty(beans.get(position).phone)) {// 如果电话是空，表示没有电话，无法进行拨打
			holder.tv_tel.setTextColor(color.textview_hint);
			holder.iv_tel.setImageResource(R.drawable.iv_tel_not_click);
			holder.rl_call.setClickable(false);
		} else {
			holder.tv_tel.setTextColor(color.edittext_input);
			holder.iv_tel.setImageResource(R.drawable.iv_tel);
			holder.rl_call.setClickable(true);
		}
		holder.rl_call.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent("android.intent.action.DIAL", Uri
						.parse("tel:"
								+ AppConstant.URLDecoderString(beans
										.get(position).phone)));
				mContext.startActivity(intent);
			}
		});
		holder.rl_tohere.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				String startAddress = SharedPreferencesUtil.getInstance(
//						mContext).getNowAddress();
				String startAddress=AppConstant.NOWADDRESS;//这是重新定位获取的地址
				Intent intent = new Intent();
				intent.setClass(mContext, RoutePlanDemo.class);
				intent.putExtra("startAddress", startAddress);
				intent.putExtra("endAddress", AppConstant
						.URLDecoderString(beans.get(position).address));
				if (!TextUtils.isEmpty(startAddress)
						&& !TextUtils.isEmpty(AppConstant
								.URLDecoderString(beans.get(position).address))) {
					mContext.startActivity(intent);
				}
			}
		});
		return convertView;

	}

	class ViewHolder {
		TextView id;// 表示排序的时候第几个
		TextView name;
		TextView juli;
		TextView addr;
		int flag;// 这个表示电话是否可以拨打
		ImageView iv_tel;
		TextView tv_tel;
		RelativeLayout rl_tohere;
		RelativeLayout rl_call;
	}

}
